<?php

/**
 * This is the model class for table "mtsys_province".
 *
 * The followings are the available columns in table 'mtsys_province':
 * @property integer $PROVINCE_ID
 * @property integer $LEVEL_N
 * @property string $PROVINCE_NAME
 */
class MtsysProvince extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return MtsysProvince the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'mtsys_province';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('PROVINCE_NAME', 'required'),
			array('LEVEL_N', 'numerical', 'integerOnly'=>true),
			array('PROVINCE_NAME', 'length', 'max'=>50),
			array('PROVINCE_NAME', 'unique'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('PROVINCE_ID, LEVEL_N, PROVINCE_NAME', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'PROVINCE_ID' => 'ID',
			'LEVEL_N' => '单位级别',
			'PROVINCE_NAME' => '单位名称',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('PROVINCE_ID',$this->PROVINCE_ID);
		$criteria->compare('LEVEL_N',$this->LEVEL_N);
		$criteria->compare('PROVINCE_NAME',$this->PROVINCE_NAME,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getUser($id)
	{
		$user = User::model()->find('unit_id=:uid and unit_level=:ul',array(':uid'=>$id,':ul'=>1));
		
		//var_dump($province_id);
	if($user){
			if(!empty($user->surname) && !empty($user->telephone)){
				return $user->surname . '-' . $user->telephone;
			}else if($user->surname == NULL && !empty($user->telephone)){				
				return $user->username . '-' . $user->telephone;
			}else if($user->surname == NULL && empty($user->telephone) ){
				return  $user->username . '-' . '暂无电话';		
			}else if(!empty($user->surname) && $user->telephone == NULL){
				return $user->surname . '-' . '暂无电话';						
			}else{
				return '暂无';
			}
		}
		return '暂无';
	}
}